package com.asion.qkan.ui.model;

import com.asion.qkan.api.ServiceApi;
import com.asion.qkan.entry.MaterialBean;
import com.asion.qkan.ui.contract.MaterialContract;
import com.asion.qkan.utils.retrofitCache.SetCacheUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by HCJ on 2016/10/17.
 */

public class MaterialModel implements MaterialContract.Model{

    @Override
    public void getMaterialDatas(String id, MaterialContract.OnMaterialListener onMaterialListener) {
        new Retrofit.Builder()
                .baseUrl("http://api.kkmh.com/")
                .client(SetCacheUtils.setCache())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ServiceApi.class)
                .getMeterial(id)
                .enqueue(new Callback<MaterialBean>() {
                    @Override
                    public void onResponse(Call<MaterialBean> call, Response<MaterialBean> response) {
                        MaterialBean materialBean = response.body();
                        onMaterialListener.onMaterialSuccess(materialBean);
                    }

                    @Override
                    public void onFailure(Call<MaterialBean> call, Throwable t) {
                        onMaterialListener.onMaterialError(t);
                    }
                });
    }
}
